package com.ruoyi.checkingin.controller;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.checkingin.service.IERpStandardService;
import com.ruoyi.common.core.domain.e_checkingin.ERpStandard;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.e_checkingin.EChecks;
import com.ruoyi.checkingin.service.IEChecksService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考勤信息Controller
 * 
 * @author JYW
 * @date 2024-01-30
 */
@RestController
@RequestMapping("/checkingin/checks")
public class EChecksController extends BaseController
{
    @Autowired
    private IEChecksService ieChecksService;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private IERpStandardService rpStandardService;

    /**
     * 查询考勤信息列表
     */
    @PreAuthorize("@ss.hasPermi('checkingin:checks:list')")
    @GetMapping("/list")
    public TableDataInfo list(EChecks eChecks)
    {
        startPage();
        //将考勤规则存入redis中，若redis没有，则在数据库中获取
        if (redisCache.getCacheList("checks") == null || redisCache.getCacheList("checks").isEmpty()){
            List<EChecks> list = ieChecksService.selectEChecksList(eChecks);
            List<EChecks> collect = list.stream().peek(eChecks1 -> {
                ERpStandard eRpStandard = rpStandardService.selectERpStandardById(eChecks1.getStage());
                eChecks1.setRemark(eRpStandard == null ? "未绑定" : eRpStandard.getRpName());
            }).collect(Collectors.toList());
            redisCache.setCacheList("checks",collect);
            redisCache.expire("checks",3, TimeUnit.MINUTES);
            return getDataTable(collect);
        }
        redisCache.expire("checks",3, TimeUnit.MINUTES);
        return getDataTable(redisCache.getCacheList("checks"));
    }

    /**
     * 导出考勤信息列表
     */
    @PreAuthorize("@ss.hasPermi('checkingin:checks:export')")
    @Log(title = "考勤信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EChecks eChecks)
    {
        List<EChecks> list = ieChecksService.selectEChecksList(eChecks);
        ExcelUtil<EChecks> util = new ExcelUtil<EChecks>(EChecks.class);
        util.exportExcel(response, list, "考勤信息数据");
    }

    /**
     * 获取考勤信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('checkingin:checks:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(ieChecksService.selectEChecksById(id));
    }

    /**
     * 新增考勤信息
     */
    @PreAuthorize("@ss.hasPermi('checkingin:checks:add')")
    @Log(title = "考勤信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EChecks eChecks)
    {
        return toAjax(ieChecksService.insertEChecks(eChecks));
    }

    /**
     * 修改考勤信息
     */
    @PreAuthorize("@ss.hasPermi('checkingin:checks:edit')")
    @Log(title = "考勤信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EChecks eChecks)
    {
        return toAjax(ieChecksService.updateEChecks(eChecks));
    }

    /**
     * 删除考勤信息
     */
    @PreAuthorize("@ss.hasPermi('checkingin:checks:remove')")
    @Log(title = "考勤信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ieChecksService.deleteEChecksByIds(ids));
    }
}
